package com.meitianhui.convenitenbuy.module.mine.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.hyphenate.ui.ChatActivity;
import com.meitianhui.convenitenbuy.module.mine.adapter.MsgGroupAdapter;
import com.meitianhui.convenitenbuy.module.mine.contract.MessageGroupContract;
import com.meitianhui.convenitenbuy.module.mine.di.DaggerMineComponent;
import com.meitianhui.convenitenbuy.module.mine.di.MineModule;
import com.meitianhui.convenitenbuy.module.mine.model.GroupBean;
import com.meitianhui.convenitenbuy.module.mine.model.GroupDetailBean;
import com.meitianhui.convenitenbuy.module.mine.model.UserUrlBean;
import com.meitianhui.convenitenbuy.module.mine.presenter.MessageGroupPresenter;
import com.meitianhui.convenitenbuy.util.TipUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
Created by amy on 2017/11/21.
 */

public class MessageGroupActivity extends BaseToolbarActivity implements MessageGroupContract.View{

    private RecyclerView mRlvGroup;
    private List<GroupBean> mGroupList = new ArrayList<>();
    private MsgGroupAdapter mMsgGroupAdapter;
    @Inject
    MessageGroupPresenter mMessageGroupPresenter;
    private ArrayList<GroupDetailBean> mGroupDetailList;

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerMineComponent.builder().mineModule(new MineModule(this)).appComponent(App.getInstance().getAppComponent()).build().inject(this);
        setTbTitle("我的群组");
        initId();
        initData();

    }

    private void initData() {
        mMsgGroupAdapter = new MsgGroupAdapter(mGroupList);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        mRlvGroup.setLayoutManager(manager);
        mRlvGroup.setAdapter(mMsgGroupAdapter);

        //获取数据
        mMessageGroupPresenter.getMsgGroup();

        mMsgGroupAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //
                String id = mGroupList.get(position).getId();

                if (TextUtils.isEmpty(id)){
                    TipUtils.showToast(MessageGroupActivity.this,"该群已解散！",3);
                }else {
                    //先要获取群组的名字，再获取到群组成员的昵称等信息 在跳转到聊天界面
                   // mMessageGroupPresenter.getChatDetailMsg(id);
                    Intent intent = new Intent(MessageGroupActivity.this, ChatActivity.class);
                    intent.putExtra("chatType", Constants.CHATTYPE_GROUP);
                    intent.putExtra("userId",id);
                    startActivity(intent);
                }
            }
        });

    }

    private void initId() {
        mRlvGroup = getViewById(R.id.rlv_group);
    }

    @Override
    protected int setView() {
        return R.layout.activity_message_group;
    }

    @Override
    protected void onRefreshData() {

    }

    @Override
    public void showError(String msg) {
        TipUtils.showToast(App.getInstance(),msg,3);

    }

    @Override
    public void showEmpty() {
        TipUtils.showToast(App.getInstance(),"暂无消息群组",3);
    }

    @Override
    public void showMsgGroup(ArrayList<GroupBean> msgGroupList) {
        this.mGroupList = msgGroupList;
        mMsgGroupAdapter.setNewData(mGroupList);
    }

    /**
     * 成功拿到群组的详细信息
     * @param groupDetailList
     * @param id
     */
    @Override
    public void getGroupDetailSucc(ArrayList<GroupDetailBean> groupDetailList, String id) {
        this.mGroupDetailList = groupDetailList;
      //  mMessageGroupPresenter.getChatMemberList(id);

    }

    @Override
    public void getGroupMemberSucc(ArrayList<UserUrlBean> userUrlList, String id) {
        UserUrlBean userUrlBean = userUrlList.get(0);

    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
}
